package cn.car91.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.activity.BaseActivity;
import cn.car91.activity.CollectionRepairshopActivity;
import cn.car91.activity.DrivingActivity;
import cn.car91.activity.SetActivity;
import cn.car91.activity.UserCommentActivity;
import cn.car91.activity.UserExposureActivity;
import cn.car91.activity.UserInfoActivity;
import cn.car91.activity.UserLoginActivity;
import cn.car91.bean.User;
import cn.car91.config.InitApplication;
import cn.car91.http.HttpHandler;
import cn.car91.service.UpdateService;
import cn.car91.utils.ImageManager;
import cn.car91.utils.RegexUtil;

import com.baidu.android.pushservice.PushManager;
import com.lidroid.xutils.util.LogUtils;

public class MenuView extends LinearLayout {
    static ImageManager imageManager;
    InitApplication config = null;
    View view = null;
    View defaultView = null;
    ImageView userImageView = null;
    TextView userNameTextView = null;
    //TextView userOtherTextView = null;
    TextView userProfileTextView = null;
    TextView userCollectionTextView = null;
    TextView userCommentTextView = null;
    TextView userExposureTextView = null;
    TextView userSuggestTextView = null;
    TextView reLoginTextView = null;
    View logoutBtn = null;
    View exitBtn = null;
    View drivingBtn = null;
    View setBtn = null;

    public MenuView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    public MenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_menu_page, this);
        config = (InitApplication) getContext().getApplicationContext();
        imageManager = new ImageManager(getContext(), "sdcard/car91/temp/user");
        User user = config.getUser();

        userImageView = (ImageView) findViewById(R.id.menu_pic);
        userNameTextView = (TextView) findViewById(R.id.menu_name);
        defaultView = findViewById(R.id.menu_person_default);
        userSuggestTextView = (TextView) findViewById(R.id.menu_suggest);
        reLoginTextView = (TextView) findViewById(R.id.menu_relogin_btn);
        view = findViewById(R.id.menu_person);
        logoutBtn = findViewById(R.id.menu_logout_btn);
        userProfileTextView = (TextView) findViewById(R.id.menu_profile_btn);
        userCollectionTextView = (TextView) findViewById(R.id.menu_collection_btn);
        userCommentTextView = (TextView) findViewById(R.id.menu_comment_btn);
        userExposureTextView = (TextView) findViewById(R.id.menu_exposure_btn);
        setBtn = findViewById(R.id.menu_set);
        exitBtn = findViewById(R.id.menu_exit);
        //drivingBtn = findViewById(R.id.menu_driving);

        if (user != null) {
            fillUserData(user);
            defaultView.setOnClickListener(onProfileClickListener);
        } else {
            if (view != null) {
                if (view.getVisibility() == View.VISIBLE) {
                    view.setVisibility(View.GONE);
                }
            }
            defaultView.setOnClickListener(onLoginClickListener);
        }

        userProfileTextView.setOnClickListener(onProfileClickListener);
        userCollectionTextView.setOnClickListener(onCollectionClickListener);
        userCommentTextView.setOnClickListener(onCommentClickListener);
        userExposureTextView.setOnClickListener(onExposureClickListener);
        logoutBtn.setOnClickListener(onLogoutClickListener);
        userSuggestTextView.setOnClickListener(onSuggestClickListener);
        reLoginTextView.setOnClickListener(onLoginClickListener);
        //drivingBtn.setOnClickListener(onDrivingClickListener);
        setBtn.setOnClickListener(onSetClickListener);
        exitBtn.setOnClickListener(onExitClickListener);

        try {
            getContext().registerReceiver(loginBroadcastReceiver, new IntentFilter(UserLoginActivity.action));
        } catch (Exception e) {
            LogUtils.e("loginBroadcastReceiver has been registed");
        }
    }

    //左侧菜单功能
    //登录
    OnClickListener onLoginClickListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getContext(), UserLoginActivity.class);
            getContext().startActivity(intent);
        }
    };

    //个人信息
    OnClickListener onProfileClickListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            ////////////////////////////////////////////////////////////////
            Intent intent = new Intent(getContext(), UserInfoActivity.class);
            getContext().startActivity(intent);
        }
    };

    //个人收藏
    OnClickListener onCollectionClickListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            ////////////////////////////////////////////////////////////
            Intent intent = new Intent(getContext(), CollectionRepairshopActivity.class);
            getContext().startActivity(intent);
        }
    };

    //个人评论
    OnClickListener onCommentClickListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            ////////////////////////////////////////////////////////////
            Intent intent = new Intent(getContext(), UserCommentActivity.class);
            getContext().startActivity(intent);
        }
    };

    //个人曝光
    OnClickListener onExposureClickListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getContext(), UserExposureActivity.class);
            getContext().startActivity(intent);
        }
    };

    //退出登录
    OnClickListener onLogoutClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            //直接Logout
            User user = config.getUser();
            if (user != null) {
                config.setUser(null);
            }

            Intent intent = new Intent(UserLoginActivity.action);
            intent.putExtra("loginStatus", false);
            getContext().sendBroadcast(intent);
        }
    };

    //意见建议
    OnClickListener onSuggestClickListener = new OnClickListener() {

        @Override
        public void onClick(View clickView) {
            final SuggestDialog.Builder builder = new SuggestDialog.Builder(getContext());
            builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    String suggest = builder.getSuggest();
                    if (RegexUtil.isNotNull(suggest)) {
                        if (config.getUser() != null) {
                        	HttpHandler.httpHandler.addSuggest(suggest, config.getUser().getId(), config.getUser().getName(), null);
                        }else{
                        	HttpHandler.httpHandler.addSuggest(suggest, null, null, null);
                        }
                        new HerilyAlertDialog.Builder(getContext()).setTitle("提示").setMessage("谢谢您的宝贵意见，我们将努力完善APP").create().show();
                    } else {
                        new HerilyAlertDialog.Builder(getContext()).setTitle("提示").setMessage("内容不能为空哦").create().show();
                    }
                }
            });
            builder.setNegativeButton("取消", null);
            builder.setTitle("您的宝贵意见");
            builder.create().show();

        }
    };

    //应用设置
    OnClickListener onDrivingClickListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getContext(), DrivingActivity.class);
            getContext().startActivity(intent);
        }
    };
    
    //应用设置
    OnClickListener onSetClickListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getContext(), SetActivity.class);
            getContext().startActivity(intent);
        }
    };

    //退出App
    OnClickListener onExitClickListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            /*MyAlertDialog.Builder builder = new MyAlertDialog.Builder(getContext());
			builder.setMessage("")*/
            UpdateService.isFirst = true;
            PushManager.stopWork(getContext());
            Intent intent = new Intent();
            intent.setAction(BaseActivity.exitAction);
            getContext().sendBroadcast(intent);
        }
    };

    private void fillUserData(User user) {
        if (user != null) {
            if (view != null)
                if (view.getVisibility() == View.GONE)
                    view.setVisibility(View.VISIBLE);
            if (userImageView != null) {
                imageManager.displayImage(config.getCenterServerUrl() + "/" + user.getPic(), userImageView, getContext().getResources().getDrawable(R.drawable.user_default));
            }
            if (userNameTextView != null) {
                userNameTextView.setText(user.getName());
            }
        }
    }

    public BroadcastReceiver loginBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            boolean loginStatus = arg1.getBooleanExtra("loginStatus", false);
            if (loginStatus) {
                User user = config.getUser();
                fillUserData(user);
                defaultView.setOnClickListener(onProfileClickListener);
            } else {
                config.setUser(null);
                defaultView.setOnClickListener(onLoginClickListener);
                if (view != null) {
                    if (view.getVisibility() == View.VISIBLE) {
                        view.setVisibility(View.GONE);
                    }
                    if (userImageView != null) {
                        userImageView.setImageResource(R.drawable.user_default);
                    }
                    if (userNameTextView != null) {
                        userNameTextView.setText(R.string.menu_name_default);
                    }
                }
            }
        }
    };
}
